﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tanmia.Core;
using Tanmia.Basis.ViewModel;
using Tanmia.Domain;
using System.ComponentModel;
using System.Windows.Data;

namespace Tanmia.Windows.PaymentScenario
{
    public class PaymentViewModel : BaseViewModel<Payment>
    {
        public ICollectionView Persons { get; private set; }
        public ICollectionView Users { get; private set; }

        private IList<Person> Castomers = new List<Person>();
        private IList<Person> Merchants = new List<Person>();

        private PersonDomain PersonObj = new PersonDomain();
        private UserDomain UserObj = new UserDomain();
        private VoucherDomain VoucherObj = new VoucherDomain();

        private static PaymentDomain PaymentObj = new PaymentDomain();

        private double total;
        public double Total
        {
            get
            {
                if (Entity.Person == Person.SelectCustomer || Entity.Person == Person.SelectMerchant || Entity.Person == null)
                    return 0;

                return total;
            }
        }

        public double Remaining
        {
            get
            {
                return Total - Entity.Value;
            }
        }


        public PaymentViewModel()
            : base(PaymentObj)
        {
            Castomers = PersonObj.GetsByType(PersonTypeEnum.Customer);
            Merchants = PersonObj.GetsByType(PersonTypeEnum.Merchant);

            UpdatePersons(PersonTypeEnum.Customer);

            Users = CollectionViewSource.GetDefaultView(UserObj.Gets());
        }

        public void UpdatePersons(PersonTypeEnum personType)
        {
            if (personType == PersonTypeEnum.Customer)
                Persons = CollectionViewSource.GetDefaultView(Castomers);
            else
                Persons = CollectionViewSource.GetDefaultView(Merchants);

            RaisePropertyChanged("Persons");
        }

        public void SetTotal()
        {
            if (Entity.Person != null)
            {
                total = VoucherObj.GetTotalAccount(Entity.Person.Account, Entity.Person.PersonType);

                RaisePropertyChanged("Total");
                RaisePropertyChanged("Remaining");
            }
        }

        public void Post()
        {
            PaymentObj.Post(Entity);
        }
    }
}
